package com.my.ruyi.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.quartz.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.my.ruyi.mapper.JobDetailMapper;
import com.my.ruyi.mapper.TriggerMapper;
import com.my.ruyi.model.JobDetailPo;
import com.my.ruyi.model.TriggerPo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/index")
@Api(value="index控制器", description="描述")
public class IndexController {
	private static final Logger log = LoggerFactory.getLogger(IndexController.class);

	@Autowired
	private JobDetailMapper jobDetailMapper;
	@Autowired
	private TriggerMapper triggerMapper;
	
	@ApiOperation(value="获取第一个", notes="第一个方法")
	@RequestMapping(value = "/first", method = RequestMethod.GET)
	public Map<String, Object> firstMethod() {
		Map<String, Object> result = new HashMap<>();
		result.put("name", "ruyi");
		return result;
	}
	
	@ApiOperation(value="添加任务")
	@RequestMapping(value = "/addJob", method = RequestMethod.POST)
	public Map<String, Object> addJobDetail(JobDetailPo jobDetailPo){
		Map<String, Object> result = new HashMap<>();
		// 如果没有输入组名，设置默认组名
		if (jobDetailPo.getJobGroupName() == null) {
			jobDetailPo.setJobGroupName(Scheduler.DEFAULT_GROUP);
		}
		// check 在同一组下是否有同名的job
		int jobDetailByGoupAndName = jobDetailMapper.getJobDetailByGoupAndName(jobDetailPo);
		if (jobDetailByGoupAndName > 0) {
			result.put("success", false);
			result.put("msg", "同组下已经存在相同的任务，请重新设置任务名称或者组名！");
			return result;
		}
		
		// 添加类
		jobDetailPo.setJobClass("com.my.ruyi.job.DemoJob");
		// 保存数据
		int uptCnt = jobDetailMapper.addJobDetail(jobDetailPo);
		result.put("result", uptCnt);
		if (uptCnt == 1) {
			result.put("success", true);
			result.put("msg", "success");
		}
		
		return result;
	}
	
	
	@ApiOperation(value="添加触发器")
	@RequestMapping(value = "/addTrigger", method = RequestMethod.POST)
	public Map<String, Object> addTrigger(TriggerPo triggerPo){
		int uptCnt = triggerMapper.addTrigger(triggerPo);
		Map<String, Object> result = new HashMap<>();
		result.put("result", uptCnt);
		if (uptCnt == 1) {
			result.put("msg", "success");
		}
		
		return result;
	}
	
	@ApiOperation(value="获取任务列表")
	@RequestMapping(value = "/get_jobs", method = RequestMethod.POST)
	public Map<String, Object> listJobDetail(String jsonString){
		List<Map<String, Object>> list = null;
		int pageIndex = 0, pageSize = 10;
		if (StringUtils.isNotBlank(jsonString)) {
			Map object = JSONObject.parseObject(jsonString, HashMap.class);
			if (object != null && !object.isEmpty()) {
				if (object.get("pageIndex") != null) {
					pageIndex = (int) object.get("pageIndex");
				}
				
				if (object.get("pageSize") != null) {
					pageSize = (int) object.get("pageSize");
				}
			}
		}
		Map<String, Object> param = new HashMap<>();
		param.put("pageIndex", pageIndex);
		param.put("pageSize", pageSize);
		list = jobDetailMapper.getListOfJobs(param);
		
		Map<String, Object> result = new HashMap<>();
		result.put("rows", list); // 此参数必须是rows,存放每页记录数
		
		if (list != null && list.size() > 0) {
			result.put("msg", "success");
			result.put("total", list.size()); // 存放总数
		}else {
			result.put("total", 0); // 存放总数
		}
		
		return result;
	}
}
